﻿using MediatR;
using Traceability.App.API.Application.Command.Enterprise;
using Traceability.App.API.DTOs;
using Traceability.App.Domain.Traceability;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructrue.Interface;

namespace Traceability.App.API.Application.Handler.Enterprise
{
    public class GetTZEnterpriseHandler : IRequestHandler<GetTZEnterpriseCommand, APIResult<TZEnterpriseDTO>>
    {
        private readonly IBaseRepository<EnterpriseModel> enterpriseRepo;

        public GetTZEnterpriseHandler(IBaseRepository<EnterpriseModel> enterpriseRepo)
        {
            this.enterpriseRepo = enterpriseRepo;
        }
        public Task<APIResult<TZEnterpriseDTO>> Handle(GetTZEnterpriseCommand request, CancellationToken cancellationToken)
        {
            APIResult<TZEnterpriseDTO> result = new APIResult<TZEnterpriseDTO>();
            var res = enterpriseRepo.GetAll().FirstOrDefault(x => x.Batch == request.Batch && x.EnterpriseId == request.EnterpriseId);
            TZEnterpriseDTO tZ = new TZEnterpriseDTO();
            if (res == null)
            {

            }
            else
            {
                tZ.Id = res.Id;
                tZ.Batch = res.Batch;
                tZ.EnterpriseId = res.EnterpriseId;
                tZ.FoundTime = res.FoundTime;
                tZ.CompanyName = res.CompanyName;
                tZ.Province = res.Province;
                tZ.City = res.City;
                tZ.Xian = res.Xian;
                tZ.CompanyProfile = res.CompanyProfile;
            }

            result.Message = "显示成功";
            result.Data = tZ;
            result.Code = APIEnum.Ok;
            return Task.FromResult(result);
        }
    }
}
